let util = require('../utils/util.js')
let klassModel = require('../models/klass.js')

let RECEIPT_REQUIREMENT = [{ "value": "confirm", "name": "无", "check": true, "desc": "" }, { "value": "text", "name": "文字", "check": false, "desc": "签收必须填写文字" }, { "value": "image", "name": "图片", "check": false, "desc": "签收必须上传图片" },
{ "value": "video", "name": "视频", "check": false, "desc": "签收必须上传视频" }]

const MY_NOTICES_KEY = 'myNoticesKey'
const CLASS_NOTICES_KEY = 'classNoticesKey'
const FILTER_NOTICES_KEY = 'filterNoticesKey'

let REVIEW_RESULT = { 'PASS': 1, 'FAIL': 2 }

// 通知列表界面过滤通知选项
let NOTICE_FILTER_CONDITION = [{ 'sid': 1, 'menuText': '查看所有', 'UIText': '所有' }, { 'sid': 2, 'menuText': '只看未签收', 'UIText': '未签收' }]

/**
 * 根据value获取receiptRequirement对象
 */
function getReceiptRequirement(value) {
  var result,length = this.RECEIPT_REQUIREMENT.length
  for (var index = 0;index <length; index++) {
    if (this.RECEIPT_REQUIREMENT[index].value == value) {
      result = this.RECEIPT_REQUIREMENT[index]
      break
    }
  }
  
  return result
}

/**
 * 获取签收内容信息
 * 根据value值，将对应的对象check置为true
 */
function getReceiptRequirements(value) {
  var result = [], length = this.RECEIPT_REQUIREMENT.length
  for (var index = 0; index < length; index++) {
    if (this.RECEIPT_REQUIREMENT[index].value == value) {
      this.RECEIPT_REQUIREMENT[index].check = true
    }else {
      this.RECEIPT_REQUIREMENT[index].check = false
    }

    result.push(this.RECEIPT_REQUIREMENT[index])
  }

  return result
}

/**
 * 获取我的通知消息
 */
function getMyNotices() {
  return wx.getStorageSync(MY_NOTICES_KEY)
}

/**
 * 设置我的通知消息
 */
function setMyNotices(notices) {
  wx.setStorageSync(MY_NOTICES_KEY, notices)
}

/**
 * 获取班级通知消息
 */
function getClassNotices() {
  return wx.getStorageSync(CLASS_NOTICES_KEY)
}

/**
 * 设置班级通知消息
 */
function setClassNotices(notices) {
  wx.setStorageSync(CLASS_NOTICES_KEY, notices)
}

/**
 * 获取通知过滤对象
 */
function getFilterCondition() {
  let condition = wx.getStorageSync(FILTER_NOTICES_KEY)
  if (condition.sid) {
    return condition
  }else {
    return NOTICE_FILTER_CONDITION[0]
  }
   
}

/**
 * 设置通知过滤对象
 */
function setFilterCondition(condition) {
  wx.setStorageSync(FILTER_NOTICES_KEY, condition)
}

/**
 * 获取系统默认生成的邀请加入通知
 * 目前认为第一条
 */
function getSystemNotice(klassSid) {
  let notices = getClassNotices()

  for (let index = 0, length = notices.length; index < length; index++) {
    console.log(notices[index])
    if (100 == notices[index].category && notices[index].klassInfo.sid == klassSid) {  // 100 为邀请接入类型的通知
      return notices[index]
    }
  }

  return null
}

/**
 * 获取某班级与群绑定的通知
 */
function getActivateNotice(klassSid) {
  let notices = getClassNotices()

  for (let index = 0, length = notices.length; index < length; index++) {
    if (101 == notices[index].category && notices[index].klassInfo.sid == klassSid) {  // 101 为绑定类型的通知
      return notices[index]
    }
  }

  return null
}

/**
 * 重置签收要求数据
 * 创建通知，切换要求时由于会改变这个值
 */
function resetReceiptRequirment() {
  this.RECEIPT_REQUIREMENT = [{ "value": "confirm", "name": "无", "check": true, "desc": "" }, { "value": "text", "name": "文字", "check": false, "desc": "签收必须填写文字" }, { "value": "image", "name": "图片", "check": false, "desc": "签收必须上传图片" },
    { "value": "video", "name": "视频", "check": false, "desc": "签收必须上传视频" }]
}

function makeUINotices(notices,max = 0) {
  let result = []

  if (null != notices) {
    var length = notices.length
    for (var index = 0; index < length; index++) {
      if (notices[index].createdAt) {
        notices[index].uiCreatedAt = util.formatDateTime(notices[index].createdAt)  // 为了界面时间按设计显示，引入uiCreatedAt属性
        notices[index].uiCreatedAtForDate = util.formatDate(notices[index].createdAt)  // 为了界面时间按设计显示，引入uiCreatedAt属性
      }

      if (index == max && max != 0) {
        break
      } else {
        result.push(notices[index])
      }
    }
  }
  return result;
}

function shareInviteNotice(source, klass, options) {
  let notice = getSystemNotice(klass.sid)

  if (notice) {
    notice.klass = klass

    return shareMessageToWeXin(source, options, notice, klass.sid, false, null)
  }
}

function showActivateClass(source,klass, options,showActivateClass = false) {
  let notice = getActivateNotice(klass.sid)
  if (notice) {
    notice.klass = klass
    
    return shareMessageToWeXin(source, options, notice, klass.sid, false, function (shareTickets) {
      if (showActivateClass) {
        getApp().globalData.param = { 'appOptions': { 'shareTicket': shareTickets }, 'notice': notice }
        wx.navigateTo({
          url: '../activateClass/activateClass?autoBind=true',
        })
      }      
    })
  }
}

/**
 * 分享消息到微信群
 */
function shareMessageToWeXin(source, options,notice,klassSid,isBind,fb = null) {
  let that = source  
  let klass = klassModel.getKlassByKlassSid(klassSid), title = notice.title
  if (klass) {
    title = klass.uiname + "：" + notice.title
  }
      
  if (true == isBind) {
    getApp().bindNoticeToClass(null, notice.sid, klassSid, function () {
      fb()
    })
  } else {
    if (fb) {      
      fb()
    }
  }    

  return {
    title: title,
    path: '/pages/noticeDetail/noticeDetail?noticeSid=' + notice.sid + '&klassSid=' + klassSid,
    // success: function (res) {
    //   // 转发成功
    //   wx.showToast({
    //     title: '分享成功',
    //     icon: 'success'
    //   })

    //   if (res.shareTickets) {        
    //     if (true == isBind) {
    //       getApp().bindNoticeToClass(res.shareTickets, notice.sid, klassSid , function () {
    //         fb()
    //       })
    //     }else {         
    //       if (fb) {
    //         fb(res.shareTickets)
    //         fb(res)
    //       }   
    //     }           
    //   }
    // },
    // fail:function(res){
    //   console.log(res)
    // }
  }
}

function updateNoticeReceiptStatus(notice,status) {
  console.log(notice)
  let currentStauts = notice.receiptStatus[0].currentStatus
  let nums = currentStauts.split('/'),failNum = 0
  if (3 == nums.length) {
    failNum = parseInt(nums[2])
    failNum = failNum + status
    if (0 == failNum) {
      currentStauts = nums[0] + '/' + nums[1]
    }else {
      currentStauts = nums[0] + '/' + nums[1] + '/' + failNum
    }
  }else {
    if (1 == status) {
      currentStauts = currentStauts + '/' + '1'
    }
  }

  notice.receiptStatus[0].currentStatus = currentStauts

  return notice
}

/**
 * 删除通知后，清理本地存储的通知数据
 */
function handleDeleteNotice(notice) {
  // 清理存储的我的通知数据
  let notices = getMyNotices()
  for (let index = 0 ,length = notices.length; index < length; index++) {
    if (notices[index].sid == notice.sid) {
      notices.splice(index,1)
      break
    }
  }
  setMyNotices(notices)  
}

module.exports = {
  RECEIPT_REQUIREMENT: RECEIPT_REQUIREMENT,
  getReceiptRequirement: getReceiptRequirement,
  getReceiptRequirements: getReceiptRequirements,
  getMyNotices: getMyNotices,
  setMyNotices: setMyNotices,
  getClassNotices: getClassNotices,
  setClassNotices: setClassNotices,
  getSystemNotice: getSystemNotice,
  resetReceiptRequirment: resetReceiptRequirment,
  makeUINotices: makeUINotices,
  showActivateClass: showActivateClass,
  shareMessageToWeXin: shareMessageToWeXin,
  getActivateNotice: getActivateNotice,
  updateNoticeReceiptStatus: updateNoticeReceiptStatus,
  REVIEW_RESULT: REVIEW_RESULT,
  shareInviteNotice: shareInviteNotice,
  NOTICE_FILTER_CONDITION: NOTICE_FILTER_CONDITION,
  FILTER_NOTICES_KEY: FILTER_NOTICES_KEY,
  getFilterCondition: getFilterCondition,
  setFilterCondition: setFilterCondition,
  handleDeleteNotice: handleDeleteNotice
}